#!/usr/bin/env ruby
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', File.dirname(__FILE__))

require 'rubygems'
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'daemons'

options = {
  :app_name   => 'alondra',
  :dir_mode   => :script,
  :dir        => 'tmp/pids'
}

Daemons.run_proc 'alondra', options do

  ENV["ALONDRA_SERVER"] = 'true'

  require_relative File.join('..', 'config', 'environment')

  Rails.logger.info "Started alondra server on port #{Alondra::Alondra.config.port}... #{EM.reactor_running?}"

  Alondra::Alondra.start_server_in_new_thread!.join

  Rails.logger.info 'Alondra server terminated'
end